package com.ruoyi.business.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.business.domain.TOrderInfo;
import com.ruoyi.business.domain.TRecipientInfo;
import com.ruoyi.business.response.ResponseData;
import com.ruoyi.business.service.BasePayment;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.HttpResponse;
import com.ruoyi.common.utils.HttpUtils;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.HashMap;

@Service("cxzfPayService")
public class CxzfPayServiceImpl extends BasePayment {




    @Override
    public JSONObject createPayOrder(TOrderInfo orderInfo) throws Exception {
        JSONObject jsonObject = new JSONObject();
        TRecipientInfo userRecipientInfo = userRecipientService.selectTRecipientInfoById(orderInfo.getRecipientId());
        HashMap<String, String> headerMap = new HashMap();
        HashMap<String, String> dataMap = new HashMap();
//        String qrcode = userRecipientInfo.getReceiptQrcode();
//        String qrcode_ = HttpUtils.getParam(qrcode,"userAppId");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String cookieStr = userRecipientInfo.getCookie();
        JSONObject cookieObj = JSONObject.parseObject(cookieStr);
//        String url = "https://cxzfvjapi.bjmymf.com/api/APPUser/GetPayTagExtList";
//        headerMap.put("token",cookieObj.getString("token"));
//        JSONObject jhxwshIdReqObj = new JSONObject();
//        jhxwshIdReqObj.put("jhxwshId",1);
//        String jhxwshIdStr = HttpUtils.sendJsonMessage(url,headerMap,jhxwshIdReqObj.toJSONString());
//        JSONObject jhxwshIdResObj = JSONObject.parseObject(jhxwshIdStr);
//        if(jhxwshIdResObj.getIntValue("code")!=0){
//            jsonObject.put("code","100");
//            jsonObject.put("msg",jhxwshIdResObj.getString("msg"));
//            return jsonObject;
//        }
//        JSONArray jhxwshIdDataList = jhxwshIdResObj.getJSONObject("data").getJSONArray("list");
//        JSONObject jhxwshIdData =jhxwshIdDataList.getJSONObject(0);
        String jhxwshIdDataUrl = cookieObj.getString("payTagUrl");
        logger.info("收银台HTMLurl："+jhxwshIdDataUrl);

        headerMap.put("User-Agent","Nebula AlipayDefined(nt:WIFI,ws:360|0|3.0) AliApp(AP/10.0.18.062203) AlipayClient/10.0.18.062203 Language/zh-Hans useStatusBar/true");


//        logger.info("qrcode："+qrcode_);
//
//        url ="https://cxzfapi.bjmymf.com/default/TagQRPay?taguuid=HXT22033020270&sign=e005b60278c40985a79a41aa69c613b0";

        HttpResponse httpResponse = HttpUtils.httpFormGet(jhxwshIdDataUrl,headerMap,"","诚享支付");
        logger.info("返回的HTML:"+httpResponse.getBody());
        String bodyStr = httpResponse.getBody();
        String userId = bodyStr.split("userId=")[1].split("&")[0];
        String wxappid = bodyStr.split("wxappid=")[1].split("&")[0];
        String alipayappid = bodyStr.split("alipayappid=")[1].split("&")[0];
        //获取用户guid
        headerMap.put("Referer","https://juhpay.bjmymf.com/");
        JSONObject guidReqObj = new JSONObject();
        guidReqObj.put("userid",userId);
        String  url = "https://mposapi.bjmymf.com/api/Pay/GetHXTOrderPayValue";
        String guidStr = HttpUtils.sendJsonMessage(url,headerMap,guidReqObj.toJSONString());
        JSONObject guidResObj = JSONObject.parseObject(guidStr);
        if(guidReqObj.getIntValue("code")!=0){
            jsonObject.put("code","100");
            jsonObject.put("msg",guidReqObj.getString("msg"));
            logger.info("========修改收款账号为不可用===============");
            userRecipientInfo.setChannelDisabled(1);
            userRecipientInfo.setDisabledInfo(guidReqObj.getString("msg"));
            userRecipientService.updateTRecipientInfo(userRecipientInfo);
            return jsonObject;
        }
        JSONObject guidData = guidResObj.getJSONObject("data");
        String merChantId = guidData.getString("merChantId");
        String zdBusChannelId = guidData.getString("zdBusChannelId");
        String guid = guidData.getString("guid");
        String mergePayOut = guidData.getString("mergePayOut");
        url = "https://mposapi.bjmymf.com/api/Pay/HXTOrderPay";
        JSONObject payReqObj = new JSONObject();
        payReqObj.put("userid",guid);
        payReqObj.put("merChantId",merChantId);
        payReqObj.put("total_fee",orderInfo.getRealMoney());
        payReqObj.put("pay_type",3);
        payReqObj.put("zdBusChannelId",zdBusChannelId);
        payReqObj.put("is_minipg",1);
        payReqObj.put("body","自选商品");
        payReqObj.put("buyer_id",orderInfo.getAliUserId());
        payReqObj.put("disGuid",null);
        payReqObj.put("country","中国");
        payReqObj.put("qrCodeType","固码");
        payReqObj.put("paySource","支付宝");
        payReqObj.put("mergePayOut",mergePayOut);
        payReqObj.put("bussinessType","1");
        //根据码牌的地址获取代理IP
        String province = userRecipientInfo.getProvince();
        String city = userRecipientInfo.getCity();
        logger.info("挂码省份："+province);

        logger.info("挂码城市："+city);
        JSONObject ipObject = commonService.getPoxyIp(orderInfo);
        String ip = ipObject.getString("ip");
        String port = ipObject.getString("port");

        payReqObj.put("latItude",ipObject.getString("latitude"));
        payReqObj.put("longItude",ipObject.getString("longitude"));
        payReqObj.put("province",province);
        payReqObj.put("city",city);
        //开始获取支付宝编码
        url = "https://mposapi.bjmymf.com/api/Pay/HXTOrderPay";
        String payRes = HttpUtils.sendJsonMessage(url,headerMap,payReqObj.toJSONString());
        JSONObject payResObj = JSONObject.parseObject(payRes);
        logger.info("获取支付宝订单号返回："+payRes);

        if(payResObj.getIntValue("code")!=0){
            jsonObject.put("code","100");
            jsonObject.put("msg",payResObj.getString("msg"));
            logger.info("========修改收款账号为不可用===============");
            userRecipientInfo.setChannelDisabled(1);
            userRecipientInfo.setDisabledInfo(payResObj.getString("msg"));
            userRecipientService.updateTRecipientInfo(userRecipientInfo);
            return jsonObject;
        }
        JSONObject payDataObj = payResObj.getJSONObject("data").getJSONObject("data");
        String transaction_id = payDataObj.getString("transaction_id");
        JSONObject payInfoObj = payDataObj.getJSONObject("pay_info");
        String tradeNO = payInfoObj.getString("tradeNO");
        jsonObject.put("payUrl",tradeNO);
        jsonObject.put("orderNo",transaction_id);
        jsonObject.put("code","200");
        return jsonObject;
    }

    public JSONObject createWxPayOrder(TOrderInfo orderInfo) throws ServiceException, UnsupportedEncodingException {
        return null;
    }


    @Override
    public ResponseData smartReissueOrder(TOrderInfo orderInfo) throws Exception {
        return new ResponseData();
    }
}
